package flex.cc.store.service.impl;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import flex.cc.PublicUtil;
import flex.cc.store.dao.ReservationOrderDao;
import flex.cc.store.dto.vo.ReservationOrderVo;
import flex.cc.store.service.ReservationOrderService;
import flex.cc.system.service.DictCodeService;
import flex.cc.system.vo.DictOptionVo;
import flex.cc.util.DateUtil;
import flex.cc.util.ExportUtil;

/**
 * 
 * @author WANGCONG
 * 2022年3月1日上午09:21:16
 * @desc 预约服务单服务实现层
 */
@Service("reservationOrderService")
public class ReservationOrderServiceImpl extends ServiceImpl<ReservationOrderDao,ReservationOrderVo> implements ReservationOrderService {

	@Resource
	private ReservationOrderDao reservationOrderDao;
	@Resource
	private DictCodeService dictCodeService;
	/**
	 * 
	 * @author WANGCONG
	 * 2022年3月1日上午09:26:41
	 * @desc 分页查询
	 */
	@Override
	public IPage<ReservationOrderVo> queryListWithPage(ReservationOrderVo vo) {
		Page<ReservationOrderVo> page = new Page<>(vo.getPageNum(),vo.getPageSize());
		IPage<ReservationOrderVo> pageData=reservationOrderDao.queryListWithPage(page, vo);
		return pageData;
	}

	/**
	 * 
	 * @author WANGCONG
	 * 2022年3月1日上午10:01:03
	 * @desc 导出
	 */
	@SuppressWarnings("deprecation")
	@Override
	public void downLoadSelection(HttpServletRequest request, HttpServletResponse response, ReservationOrderVo vo,
			String fileParentPath, String fileName, boolean hasPerms) {
		// 表名、表头数据
        String title = fileName;
        String[] headers = {"服务项目","服务项目编码","服务订单号","订单状态","预约人","手机号","车牌号","服务商","卡券号","卡券状态","订单时间","卡券失效日期"};
        String[] fields = {"activityName","activityId","orderNo","orderStatus","contacts","tel","carNo","serverName","cardNo","cardStatus","createTimeString","expDateString"};
        List<ReservationOrderVo> records = reservationOrderDao.queryList(vo);
        if (!PublicUtil.isEmpty(records)) {
        	List<DictOptionVo> cardStatuss = dictCodeService.getSelectOptions("couponStatus", "1");   //卡券状态
        	//服务类型  1-代驾类  2-护理类
        	String activityType = vo.getActivityType();
        	List<DictOptionVo> driveStatuss = null;
        	if(activityType.equals("1")){
        		driveStatuss = dictCodeService.getSelectOptions("reservationOrderStatus", "1");   //代驾类预约单订单状态
        	}else if(activityType.equals("2")){
        		driveStatuss = dictCodeService.getSelectOptions("nursingOrderStatus", "1");   //护理类预约单服务进度
        	}
        	List<DictOptionVo> activityNames = dictCodeService.getSelectOptions("activityName", "1"); //服务项目
        	Map<String,String> cardStatusMap = cardStatuss.stream().collect(Collectors.toMap(e -> e.getValue(),e -> e.getLabel()));
        	Map<String,String> orderStatusMap = driveStatuss.stream().collect(Collectors.toMap(e -> e.getValue(),e -> e.getLabel()));
        	Map<String,String> activityNameMap = activityNames.stream().collect(Collectors.toMap(e -> e.getValue(),e -> e.getLabel()));
        	for(int i=0; i<records.size(); i++){
        		ReservationOrderVo order = records.get(i);
        		//替换服务项目、订单状态、卡券状态
        		order.setActivityName(activityNameMap.get(order.getActivityId()));
        		order.setOrderStatus(orderStatusMap.get(order.getOrderStatus()));
        		order.setCardStatus(cardStatusMap.get(order.getCardStatus()));
        		order.setCreateTimeString(DateUtil.DateToString(order.getCreateTime()));
        		order.setExpDateString(DateUtil.DateToString(order.getExpDate()));
        		String tel = order.getTel();
        		if(!hasPerms && PublicUtil.isNotEmpty(tel)){//脱敏手机号
        			if(StringUtils.isNotEmpty(tel)){
        				tel = tel.replaceAll("(\\w{3})\\w*(\\w{4})", "$1****$2");
        				order.setTel(tel);
        	        }
        		}
        	}
            try {
                SXSSFWorkbook wb = ExportUtil.createWorkbook(10, false);//创建工作簿对象
                ExportUtil.exportExcel(title, headers, fields, 0, wb, records);
                File fileParent = new File(fileParentPath);
                if (!fileParent.exists()) { // 若目录不存在则创建
                    fileParent.mkdirs();
                }
                File file = new File(fileParentPath + File.separator + fileName + ".xlsx");
                if (file.exists()) { // 若文件已存在则删除
                    file.delete();
                }
                try {
                    wb.write(new FileOutputStream(file));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
	}



}
